/*
 * Copyright (c) 2024 Black Sesame Technologies
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "dal_fastdds/listener_impl/TopicListenerImpl.hpp"
#include "dal_fastdds/entity_impl/DalTopicImpl.hpp"
#include "dal_fastdds/utils/log.hpp"
#include "dal_fastdds/utils/EntityMappingManager.hpp"

TopicListenerImpl::TopicListenerImpl(dal_topic_listener_callbacks_t callbacks)
    : callbacks_(callbacks)
{
}

TopicListenerImpl::~TopicListenerImpl() {}

void TopicListenerImpl::on_inconsistent_topic(Topic* topic, InconsistentTopicStatus status)
{
    if (callbacks_.on_inconsistent_topic == nullptr)
        return;

    dal_topic_t* dal_topic = EntityMappingGetTopic(topic);
    if (!dal_topic) {
        return;
    }

    dal_inconsistent_topic_status_t dal_status;
    StatusConverter::GetInstance()->ConvertInconsistentTopicStatusFromFastddsToDal(&status,
                                                                                   &dal_status);

    void* data = callbacks_.data_;
    (callbacks_.on_inconsistent_topic)(dal_topic, &dal_status, data);
}